package com.sxhalo.fsms.controller.api;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.FactDeviceSpecificAttr;
import com.sxhalo.fsms.domain.SysDeviceAttr;
import com.sxhalo.fsms.log.SysControllerLog;
import com.sxhalo.fsms.service.IFactDeviceSpecificAttrService;
import com.sxhalo.fsms.service.ISysDeviceAttrService;
import com.sxhalo.fsms.util.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;

@Controller
@RequestMapping("/api")
public class SysDeviceAttrController extends BaseController {

	@Autowired
	private ISysDeviceAttrService attrService;
	@Autowired
	private IFactDeviceSpecificAttrService specificAttrService
            ;

	@ResponseBody
	@RequestMapping(value = "/device/attr", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findDeviceAttr(Integer current, Integer pageSize,Integer deviceId, HttpServletRequest req) {
		String where = "delete_flag=0";
		String order = " device_type_id  asc, order_no ";
		if (!isBlank(req.getParameter("deviceTypeId"))) {
			where += " and device_type_id like '%" + req.getParameter("deviceTypeId").trim() + "%'";
		}
		if (!isBlank(req.getParameter("attrType"))) {
			where += " and attr_type = '" + req.getParameter("attrType").trim() + "'";
		}
		if (!isBlank(req.getParameter("browseName"))) {
			where += " and browse_name like '%" + req.getParameter("browseName").trim() + "%'";
		}
		if (!isBlank(req.getParameter("useState"))) {
			where += " and use_state = " + req.getParameter("useState").trim() ;
		}
       //设计页面获取当前设备的所有变量
		if(deviceId!=null){
			where += " and attr_type='common' or   attr_id in (select attr_id from fact_device_specific_attr where delete_flag=0  and use_state=1 and device_id= "+deviceId+" )  ";
		}
		return findList(current, pageSize, where, order, attrService);
	}


	@ResponseBody
	@RequestMapping(value = "/id", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findSpecialDeviceAttr(Integer deviceId, Integer deviceTypeId, HttpServletRequest req) {
		String where = "delete_flag=0 and device_type_id="+deviceTypeId+" and attr_type='special' and   attr_id not in ( select attr_id from  fact_device_specific_attr where device_id="+deviceId+")";

		return ApiResult.retOK(req,attrService.findWhereList(where," order_no"));
	}


	@ResponseBody
	@SysControllerLog(operationName = "添加", modelName = "设备变量")
	@RequestMapping(value = "/device/attr", method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	public ApiResult saveDeviceAttr( SysDeviceAttr record, String  ids, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		record.setDeleteFlag(0);
		record.setOrderNo(attrService.getMaxNo(record.getDeviceTypeId()));
		record.setCreateTime(nowtime);
		attrService.insertSelective(record);
//		if(record.getAttrType().equals("special")&&ids!=null&&!ids.equals("")){
//			FactDeviceSpecificAttr specificAttr=new FactDeviceSpecificAttr();
//			specificAttr.setAttrId(record.getAttrId());
//			specificAttr.setDeviceTypeId(record.getDeviceTypeId());
//			specificAttr.setUseState(record.getUseState());
//			specificAttr.setUpdateTime(nowtime);
//			specificAttr.setDeleteFlag(0);
//			specificAttr.setCreateTime(nowtime);
//			specificAttrService.batchInsert(specificAttr,ids);
//		}
		return ApiResult.retOK(req);
	}


	@ResponseBody
	@SysControllerLog(operationName = "更新", modelName = "设备变量")
	@RequestMapping(value = "/device/attr", method = RequestMethod.PUT, produces="application/json;charset=UTF-8")
	public ApiResult updateDeviceAttr(@RequestBody  SysDeviceAttr record, String method, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		attrService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "删除", modelName = "设备变量")
	@RequestMapping(value = "/device/attr", method = RequestMethod.DELETE, produces="application/json;charset=UTF-8")
	public ApiResult delDeviceAttr(Integer attrId, String method, HttpServletRequest req) {
		SysDeviceAttr record =new SysDeviceAttr();
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setDeleteFlag(1);
		record.setAttrId(attrId);
		record.setUpdateTime(nowtime);
		attrService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "批量删除", modelName = "设备变量")
	@RequestMapping(value = "/device/attr/batchdel", method = RequestMethod.DELETE, produces="application/json;charset=UTF-8")
	public ApiResult batchDelDeviceAttr(String  ids, String method, HttpServletRequest req) {
       attrService.deleteByWhere("attr_id in ("+ids+")");
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@RequestMapping(value = "/device/specific/attr", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findDeviceAllAttr(Integer current, Integer pageSize,Integer id, HttpServletRequest req) {
        String where = "a.delete_flag=0";
		if(id!=null){
			where+= " and a.device_id="+id;
		}
		return findList(current, pageSize, where, "", specificAttrService);
	}








	@ResponseBody
	@SysControllerLog(operationName = "添加", modelName = "设备私有变量")
	@RequestMapping(value = "/device/specific/attr", method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	public ApiResult saveDeviceAttr( FactDeviceSpecificAttr record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUseState(record.getUseState());
		record.setUpdateTime(nowtime);
		record.setDeleteFlag(0);
		record.setCreateTime(nowtime);
		specificAttrService.insertOrUpdate(record);
		return ApiResult.retOK(req);
	}



	@ResponseBody
	@SysControllerLog(operationName = "更新", modelName = "设备私有变量")
	@RequestMapping(value = "/device/specific/attr", method = RequestMethod.PUT, produces="application/json;charset=UTF-8")
	public ApiResult updateDeviceSpecificAttr(@RequestBody  FactDeviceSpecificAttr record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		specificAttrService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "删除", modelName = "设备私有变量")
	@RequestMapping(value = "/device/specific/attr", method = RequestMethod.DELETE, produces="application/json;charset=UTF-8")
	public ApiResult delDeviceSpecificAttr(FactDeviceSpecificAttr record, HttpServletRequest req) {
		specificAttrService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@RequestMapping(value = "/device/attr/list", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findlist(HttpServletRequest req,Integer deviceId,Integer typeId,Integer dataType) {
		String where="";
		if(dataType!=null&&dataType==1){
			where+=" and data_type ='int' or  data_type='decimal'";
		}
		List<SysDeviceAttr> list = attrService.findWhereList(" delete_flag=0 and use_state=1 and attr_type='common'"+(typeId==null?" ":"  and device_type_id = "+typeId )+(deviceId==null?"":"  or attr_id in (select attr_id from fact_device_specific_attr where delete_flag=0 and use_state=1 and device_id="+deviceId+")") +where, "attr_id");
		return ApiResult.retOK(req, list);
	}


}
